1 00:00:00,120 --> 00:00:03,630 Now let's save off our equipment to our data store. 2 00:00:03,660 --> 00:00:05,310 I am in the game manager. 3 00:00:05,310 --> 00:00:08,550 The game manager is and server script service game manager. 4 00:00:08,550 --> 00:00:15,960 And then we'll go near the top of the script where we're doing our declaration of variables, local 5 00:00:16,140 --> 00:00:19,890 equip, and then I'll just do equip utils. 6 00:00:19,890 --> 00:00:30,360 We're going to require our module script game server, script service and then equip storage utils. 7 00:00:30,810 --> 00:00:31,230 Cool. 8 00:00:31,230 --> 00:00:34,410 Now we can call stuff from our equipment utils. 9 00:00:34,410 --> 00:00:39,510 Let's go down to our initialized player data. 10 00:00:39,990 --> 00:00:41,760 And in here. 11 00:00:42,510 --> 00:00:45,360 If we get data they played. 12 00:00:45,360 --> 00:00:48,720 So we need to initialize the player who's entering the game. 13 00:00:48,840 --> 00:00:57,660 Let's go down below our streak information and I'm going to do an if statement here. 14 00:00:57,660 --> 00:00:59,130 I'll do if. 15 00:01:00,600 --> 00:01:16,740 Data equipment data dot equipment then equip utils colon update player with data info. 16 00:01:17,160 --> 00:01:18,000 There we go. 17 00:01:18,300 --> 00:01:25,800 I'm going to do a player comma whoops data dot equipment. 18 00:01:26,370 --> 00:01:32,610 So if we have equipment then we're going to go over to the module script and update our equipment table. 19 00:01:32,610 --> 00:01:34,010 With that equipment. 20 00:01:34,020 --> 00:01:42,390 Let's go to our equipment storage utils and look for that, that method and there's nothing in it. 21 00:01:42,390 --> 00:01:44,520 So we're going to have to do that now. 22 00:01:45,730 --> 00:01:48,730 Turns out this is going to be incredibly easy. 23 00:01:48,880 --> 00:01:55,990 We're getting our data, which is our data equipment, and then we'll get our equipment table for the 24 00:01:55,990 --> 00:02:02,250 player equals data, and we're done with that. 25 00:02:02,260 --> 00:02:03,580 That's pretty cool. 26 00:02:04,060 --> 00:02:05,850 This one's going to be a little trickier. 27 00:02:05,860 --> 00:02:12,160 Let's work on this now because we're going to implement that in a game manager when we die or when we're 28 00:02:12,160 --> 00:02:14,890 coming into the world, even if it's the first time. 29 00:02:14,890 --> 00:02:19,830 So this one is going to be the hardest of them, but it's still not that bad. 30 00:02:19,840 --> 00:02:24,490 We're going to say if equipment table. 31 00:02:26,710 --> 00:02:33,910 For the player, then let's get the character from the player. 32 00:02:34,060 --> 00:02:44,080 Character equals I'll do char equals character or player dot character added. 33 00:02:44,080 --> 00:02:47,170 Wait, just in case the character is being loaded. 34 00:02:47,380 --> 00:02:48,760 I get that spelled right. 35 00:02:48,760 --> 00:02:50,620 All right, so we get the character either way. 36 00:02:50,620 --> 00:02:51,520 Cool. 37 00:02:51,910 --> 00:02:55,060 And then I'm going to do an if statement. 38 00:02:55,180 --> 00:03:00,220 No, let's do a for let's do four I and V in pairs. 39 00:03:01,180 --> 00:03:02,290 There we go. 40 00:03:02,590 --> 00:03:08,080 We'll get our equipment table square bracket player. 41 00:03:08,590 --> 00:03:16,210 We're going to get all the elements in there and do we'll do a variable local. 42 00:03:16,210 --> 00:03:21,160 I'm going to call this backpack item. 43 00:03:21,160 --> 00:03:25,210 We have to check to see if they have the item already. 44 00:03:25,210 --> 00:03:31,360 This is going to have a race condition where when we're entering the world, it might get called more 45 00:03:31,360 --> 00:03:32,260 than one time. 46 00:03:32,260 --> 00:03:38,530 So we have to make the function item potent, which means you can call it as many times as you want, 47 00:03:38,530 --> 00:03:44,530 as long as you call it once, it's not going to make a difference if you call it two times or ten times. 48 00:03:44,530 --> 00:03:57,940 So we're going to get our player backpack colon, find first child V dot name. 49 00:04:01,830 --> 00:04:02,310 All right. 50 00:04:02,310 --> 00:04:08,850 And then we'll do local item. 51 00:04:10,580 --> 00:04:12,470 And that's going to be a quip. 52 00:04:12,490 --> 00:04:19,940 Diatom char colon Find first child. 53 00:04:20,590 --> 00:04:23,680 V dot name. 54 00:04:23,680 --> 00:04:26,320 So we're going to check to see if it's in the backpack. 55 00:04:26,320 --> 00:04:28,570 We're going to check to see if it's equipped. 56 00:04:29,020 --> 00:04:39,850 Right now what we have to do is say, if not be P item, not backpack item and not E item not equipped 57 00:04:39,850 --> 00:04:40,630 item. 58 00:04:40,630 --> 00:04:46,780 Then local item equals replicated storage. 59 00:04:46,840 --> 00:04:56,860 Wait for child V name clone. 60 00:04:58,730 --> 00:05:00,500 And then we would also. 61 00:05:00,500 --> 00:05:02,750 Oh, don't we have replicated storage here? 62 00:05:03,050 --> 00:05:04,610 We don't. 63 00:05:04,820 --> 00:05:05,540 That's all right. 64 00:05:06,170 --> 00:05:10,430 Let's go ahead and get a variable for us in case we need it elsewhere. 65 00:05:10,430 --> 00:05:11,950 We'll put it at the top. 66 00:05:11,960 --> 00:05:16,940 Scroll on up here, maybe underneath our equipment table. 67 00:05:16,940 --> 00:05:22,280 Local R's game, get service, replicated storage. 68 00:05:22,280 --> 00:05:23,960 Now we can get our items. 69 00:05:24,470 --> 00:05:25,660 Let's go back. 70 00:05:25,670 --> 00:05:26,690 Ah, here we go. 71 00:05:26,690 --> 00:05:28,490 So we're going to clone the item. 72 00:05:28,490 --> 00:05:31,880 This is also where we would check for configuration stuff. 73 00:05:31,880 --> 00:05:36,770 We don't have any of that implemented, but when we start adding it, we'll start doing it. 74 00:05:37,310 --> 00:05:42,320 We have the cooldown, but the cooldown is always the same on the pistol, so we're not going to have 75 00:05:42,320 --> 00:05:43,430 a problem with that. 76 00:05:43,790 --> 00:05:52,160 Once we clone our item, we'll put the parent as the player bat backpack, so we're going to stick the 77 00:05:52,160 --> 00:05:57,260 item in the backpack when they enter the game, either if they die or for the first time. 78 00:05:58,130 --> 00:05:59,510 All right, we got one more. 79 00:05:59,510 --> 00:06:00,770 We may as well do it right. 80 00:06:00,770 --> 00:06:02,660 We have this remove player. 81 00:06:03,170 --> 00:06:05,120 This is actually really easy to. 82 00:06:05,210 --> 00:06:11,390 We're going to get our equipment table for the player and then we're just going to equal nil. 83 00:06:11,390 --> 00:06:17,090 And that's going to remove all the data for that player when they leave the game so that our table doesn't 84 00:06:17,090 --> 00:06:18,350 grow ad infinitum. 85 00:06:18,350 --> 00:06:21,230 Well, or until the server is turned off and reset. 86 00:06:21,680 --> 00:06:22,760 Nice. 87 00:06:23,000 --> 00:06:24,650 I'm going to get rid of that line there. 88 00:06:24,980 --> 00:06:29,710 Let's add one more simple function to our equip storage utils. 89 00:06:29,720 --> 00:06:30,350 I'm going to. 90 00:06:30,350 --> 00:06:37,790 I'm going to add it below equip player and above remove player, but it doesn't really matter where 91 00:06:37,790 --> 00:06:38,630 you add it. 92 00:06:39,290 --> 00:06:54,710 Let's do a function equip utils and I'm going to call this get player items to save and we'll pass in 93 00:06:54,710 --> 00:06:55,760 the player. 94 00:06:57,410 --> 00:07:04,770 And then also I'm want to do is return the equipment table for that player. 95 00:07:04,790 --> 00:07:07,700 That's going to make things a little simpler on our game manager. 96 00:07:07,700 --> 00:07:12,030 That way we don't have to do data storage inside this Utils script. 97 00:07:12,050 --> 00:07:14,870 Let's go back to our game manager now. 98 00:07:15,530 --> 00:07:21,230 Let's go down to save player data. 99 00:07:21,630 --> 00:07:21,970 All right? 100 00:07:22,010 --> 00:07:23,900 So if you do save player data. 101 00:07:25,090 --> 00:07:29,130 We need to save off of our save off our equipment. 102 00:07:29,140 --> 00:07:32,410 I'm going to do it under the Daily Streak stuff. 103 00:07:33,190 --> 00:07:36,970 And oh, man, I made it lowercase. 104 00:07:36,970 --> 00:07:37,660 That's all right. 105 00:07:37,660 --> 00:07:43,300 So we got our we have our equipment, which I messed up the case. 106 00:07:43,300 --> 00:07:45,130 Just keep it consistent. 107 00:07:45,130 --> 00:07:46,930 You can change all that if you want. 108 00:07:46,930 --> 00:07:47,740 Make it an uppercase. 109 00:07:47,740 --> 00:07:49,860 But I'm already this far in. 110 00:07:49,870 --> 00:07:52,540 I'm going to say data dot equipment. 111 00:07:52,540 --> 00:07:55,570 Then I'm going to get my what would you call it? 112 00:07:55,570 --> 00:07:57,250 Equipment utils. 113 00:07:58,120 --> 00:08:01,840 And I'm going to get player items to save. 114 00:08:02,020 --> 00:08:04,510 And there's my player, right? 115 00:08:04,510 --> 00:08:12,700 So that is going to put all of my equipment inside that data and then we're going to do our set async 116 00:08:12,700 --> 00:08:13,740 wallah. 117 00:08:13,780 --> 00:08:15,580 It is saved. 118 00:08:17,120 --> 00:08:22,190 When we're doing a player removing let's go down to. 119 00:08:23,750 --> 00:08:25,490 Player removing. 120 00:08:25,490 --> 00:08:28,920 Look, the only thing we do when we do player removing is save the data. 121 00:08:28,940 --> 00:08:30,760 We're gonna have to change this a little bit. 122 00:08:30,770 --> 00:08:34,250 We're going to have to do a function. 123 00:08:34,550 --> 00:08:41,930 The player will get passed in and then we kind of messed that up. 124 00:08:42,330 --> 00:08:44,060 Got to put an end here. 125 00:08:45,880 --> 00:08:48,100 Save player data, that's fine. 126 00:08:48,100 --> 00:08:52,690 But we now have to pass in the player because we didn't just connect the function, we connected the 127 00:08:52,690 --> 00:08:54,700 function to an anonymous function. 128 00:08:56,080 --> 00:08:59,620 Now I want to get my equipment utils. 129 00:09:01,320 --> 00:09:05,430 And remove the player from the equipment table. 130 00:09:06,180 --> 00:09:07,230 There we go. 131 00:09:07,770 --> 00:09:10,830 All right, one more thing we got to do. 132 00:09:11,610 --> 00:09:11,910 I know. 133 00:09:11,910 --> 00:09:13,650 I probably keep saying that. 134 00:09:14,550 --> 00:09:17,340 Let's go to our player added event. 135 00:09:17,340 --> 00:09:25,320 All right, so player is added, we add the board, we initialize the player data, meaning we get any 136 00:09:25,320 --> 00:09:27,090 equipment data that's already there. 137 00:09:27,090 --> 00:09:36,900 So what I can do here is equip utils, equip player. 138 00:09:38,410 --> 00:09:40,330 And then pass in the player. 139 00:09:40,420 --> 00:09:46,540 Now, here's why we needed something that was and potent for our equipped player. 140 00:09:46,540 --> 00:09:52,750 We need to be able to call it more than once when we connect our character added because initialized 141 00:09:52,750 --> 00:09:54,950 player data could take a little bit of time. 142 00:09:54,970 --> 00:10:01,390 This might get added first, but we have to make sure that we add this. 143 00:10:02,240 --> 00:10:09,740 Control c to r character added event because when we die we want to get our equipment. 144 00:10:10,010 --> 00:10:11,480 Where should I add that? 145 00:10:11,480 --> 00:10:13,040 I could add that at the top. 146 00:10:13,040 --> 00:10:15,110 So we have our character added. 147 00:10:17,160 --> 00:10:19,290 And then we'll just paste that again. 148 00:10:19,620 --> 00:10:24,720 So if you don't have this in both spots, sometimes when you enter the world, you're not going to get 149 00:10:24,720 --> 00:10:25,470 equipment. 150 00:10:25,650 --> 00:10:31,440 The player, the character will get added and you're going to get a race condition. 151 00:10:31,440 --> 00:10:33,690 So you're going to need it for both places. 152 00:10:33,690 --> 00:10:37,510 So you have that's why you have to do the checks to make sure that the equipment is there. 153 00:10:37,530 --> 00:10:39,320 I think we're good to go. 154 00:10:39,330 --> 00:10:43,230 Let's play our game and see if it functions correctly. 155 00:10:46,910 --> 00:10:50,630 Now we're not going to have anything starting out because we weren't saving anything. 156 00:10:50,630 --> 00:10:52,830 Let's take a look at our output window. 157 00:10:52,850 --> 00:10:54,440 The output window looks good. 158 00:10:54,440 --> 00:10:55,700 We don't have any errors. 159 00:10:55,700 --> 00:11:01,100 I'll just make this a little smaller and then if something interesting pops up, I'll make it bigger. 160 00:11:01,100 --> 00:11:04,400 We're going to buy simple pistol. 161 00:11:05,680 --> 00:11:06,670 There we go. 162 00:11:06,700 --> 00:11:09,640 We're going to buy Fabulous Lemon. 163 00:11:10,120 --> 00:11:11,230 There we go. 164 00:11:11,470 --> 00:11:13,720 We are going to die. 165 00:11:15,910 --> 00:11:16,300 Yeah. 166 00:11:16,330 --> 00:11:17,170 Reset. 167 00:11:20,630 --> 00:11:21,730 There we go. 168 00:11:21,740 --> 00:11:22,450 We got it. 169 00:11:22,460 --> 00:11:23,430 We got it. 170 00:11:23,450 --> 00:11:25,700 Now, if we leave the game. 171 00:11:29,950 --> 00:11:31,330 Started back up. 172 00:11:36,420 --> 00:11:38,820 I look at that and no errors. 173 00:11:39,570 --> 00:11:41,670 So we have some data storage. 174 00:11:42,270 --> 00:11:43,860 That's pretty cool. 175 00:11:45,570 --> 00:11:45,870 All right. 176 00:11:45,870 --> 00:11:47,940 I'll see you in the next video.